library(haven)
library(readstata13)
library(plm)
library(AER)
library(foreign)
library(corrgram)
library(reshape)
library(ggplot2)
library(ggthemes)
library(Rarity)

donate <- read.dta13("Barber_Eatough_Replication_JOP.dta")

#Figure A1
par(mar = c(5, 6, 3, 2))
plot(donate$politicization, donate$number_articles_denominator, pch = 16, 
     xlab = "Politicization Score\n(% of Articles Also Mentioning Republican Or Democrat)",
     ylab = "Number of Articles Mentioning Issue Area\n(in thousands)", 
     main = "Correlation Between Mentions and Politicization",
     axes = F)
text(50, 100000, "Cor = -.05", cex = 2)
cor(donate$politicization, donate$number_articles_denominator, use = "complete.obs")
axis(2, at = seq(0, 150000, 25000), label = seq(0, 150, 25), las = 2)
axis(1, at = seq(0, 70, 10))
box()




donate00 <- donate[donate$CYCLE == 2000,]
donate00 <- donate00[order(donate00$CODE5A),]
donate00$N <- unlist(tapply(donate00$CODE5A, donate00$CODE5A, function(x) 1:length(x)))
donate00 <- donate00[donate00$N == 1,]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2002])
table(is.na(m))
donate00$score02 <- donate$politicization[donate$CYCLE == 2002][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2004])
table(is.na(m))
donate00$score04 <- donate$politicization[donate$CYCLE == 2004][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2006])
table(is.na(m))
donate00$score06 <- donate$politicization[donate$CYCLE == 2006][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2008])
table(is.na(m))
donate00$score08 <- donate$politicization[donate$CYCLE == 2008][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2010])
table(is.na(m))
donate00$score10 <- donate$politicization[donate$CYCLE == 2010][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2012])
table(is.na(m))
donate00$score12 <- donate$politicization[donate$CYCLE == 2012][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2014])
table(is.na(m))
donate00$score14 <- donate$politicization[donate$CYCLE == 2014][m]


donate00 <- donate00[order(donate00$politicization),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$politicization, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2000)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score02),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score02, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2002)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score04),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score04, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2004)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score06),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score06, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2006)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score08),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score08, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2008)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score10),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score10, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2010)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score12),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score12, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2012)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,75))
ytick <- seq(1,74, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)

donate00 <- donate00[order(donate00$score14),]
donate00 <- donate00[!is.na(donate00$score14),]
donate00$yaxis <- seq(1:nrow(donate00))
par(mar = c(4, 10, 4, 2))
plot(donate00$score14, donate00$yaxis, pch=16, cex=1, main = "Industry Politicization (2014)", 
     xlab="Politicization Score", bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, 
     xlim = c(0, 100), ylim=c(0,74))
ytick <- seq(1,73, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=donate00$INDUSTRY, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)




#Leave one category out - DV: share non-incumbent
fe <- unique(donate$CODE5A)
coefs <- NULL
for(i in 1:length(fe)){
  model1 <- lm(share_nonincumbent ~ as.factor(CYCLE) + as.factor(CODE5A) + log_num_donations + log_donations + 
                 numgroups + log_groupmoney + log_articles + politicization, 
               data = donate[donate$numberofdonations > 4 & donate$CODE5A != fe[i],])
  
  a <- model1$coefficients[length(model1$coefficients)]   
  coefs <- c(coefs, a)
  print(i)
}

par(mar = c(4, 4, 3, 2))
plot(hist(coefs, breaks = 10), xlim = c(0, .25), col = "grey", axes = F, 
     xlab = "Coefficient Size - Industry Politicization", 
     ylab = "Frequency", main = "DV: Share to Non-Incumbents")
axis(1, at = c(seq(0, .25, .05)))
axis(2, at = c(seq(0, 40, 5)), las = 2)
abline(h = 0)



#Leave one category out - DV: ave district margin
coefs <- NULL
for(i in 1:length(fe)){
  model1 <- lm(avgdistrictmargin ~ as.factor(CYCLE) + as.factor(CODE5A) + log_num_donations + log_donations + 
                 numgroups + log_groupmoney + log_articles + politicization, 
               data = donate[donate$numberofdonations > 9 & donate$CODE5A != fe[i],])
  
  a <- model1$coefficients[length(model1$coefficients)]   
  coefs <- c(coefs, a)
  print(i)
}

plot(hist(coefs, breaks = 10), xlim = c(-.02, 0.02), col = "grey", axes = F, 
     xlab = "Coefficient Size - Industry Politicization", 
     ylab = "Frequency", main = "DV: Ave. District Competitiveness")
axis(1, at = c(seq(-.02, 0.02, .01)))
axis(2, at = c(seq(0, 25, 5)), las = 2)
abline(h = 0)



#Leave one category out - DV: share to majority party
coefs <- NULL
for(i in 1:length(fe)){
  model1 <- lm(share_majority ~ as.factor(CYCLE) + as.factor(CODE5A) + log_num_donations + log_donations + 
                 numgroups + log_groupmoney + log_articles + politicization, 
               data = donate[donate$numberofdonations > 4 & donate$CODE5A != fe[i],])
  
  a <- model1$coefficients[length(model1$coefficients)]   
  coefs <- c(coefs, a)
  print(i)
}

plot(hist(coefs, breaks = 10), xlim = c(-.5, 0), col = "grey", axes = F, 
     xlab = "Coefficient Size - Industry Politicization", 
     ylab = "Frequency", main = "DV: Majority Party Donations")
axis(1, at = c(seq(-.5, 0, .05)))
axis(2, at = c(seq(0, 40, 5)), las = 2)
abline(h = 0)



#Leave one category out - DV: share to committee chairs
coefs <- NULL
for(i in 1:length(fe)){
  model1 <- lm(share_chair ~ as.factor(CYCLE) + as.factor(CODE5A) + log_num_donations + log_donations + 
                 numgroups + log_groupmoney + log_articles + politicization, 
               data = donate[donate$numberofdonations > 4 & donate$CODE5A != fe[i],])
  
  a <- model1$coefficients[length(model1$coefficients)]   
  coefs <- c(coefs, a)
  print(i)
}

plot(hist(coefs, breaks = 10), xlim = c(-.1, 0), col = "grey", axes = F, 
     xlab = "Coefficient Size - Industry Politicization", 
     ylab = "Frequency", main = "DV: Committee Chair Donations")
axis(1, at = c(seq(-.1, 0, .01)), cex.axis = 1)
axis(2, at = c(seq(0, 30, 5)), las = 2)
abline(h = 0)





